package com.ruoyi.basedata.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.OmsOrgunitMapper;
import com.ruoyi.basedata.domain.OmsOrgunit;
import com.ruoyi.basedata.service.IOmsOrgunitService;

/**
 * 结算组织Service业务层处理
 * 
 * @author xiefeng
 * @date 2023-02-17
 */
@Service
public class OmsOrgunitServiceImpl implements IOmsOrgunitService 
{
    @Autowired
    private OmsOrgunitMapper omsOrgunitMapper;

    /**
     * 查询结算组织
     * 
     * @param id 结算组织主键
     * @return 结算组织
     */
    @Override
    public OmsOrgunit selectOmsOrgunitById(Long id)
    {
        return omsOrgunitMapper.selectOmsOrgunitById(id);
    }

    /**
     * 查询结算组织
     *
     * @param Number 结算组织编码
     * @return 结算组织
     */
    @Override
    public OmsOrgunit selectOmsOrgunitByNumber(String Number)
    {
        return omsOrgunitMapper.selectOmsOrgunitByNumber(Number);
    }

    /**
     * 查询结算组织列表
     * 
     * @param omsOrgunit 结算组织
     * @return 结算组织
     */
    @Override
    public List<OmsOrgunit> selectOmsOrgunitList(OmsOrgunit omsOrgunit)
    {
        return omsOrgunitMapper.selectOmsOrgunitList(omsOrgunit);
    }

    /**
     * 新增结算组织
     * 
     * @param omsOrgunit 结算组织
     * @return 结果
     */
    @Override
    public int insertOmsOrgunit(OmsOrgunit omsOrgunit)
    {
        if (initOrgUnit(omsOrgunit)) return -1;
        return omsOrgunitMapper.insertOmsOrgunit(omsOrgunit);
    }

    /**
     * 修改结算组织
     * 
     * @param omsOrgunit 结算组织
     * @return 结果
     */
    @Override
    public int updateOmsOrgunit(OmsOrgunit omsOrgunit)
    {
        if (initOrgUnit(omsOrgunit)) return -1;
        return omsOrgunitMapper.updateOmsOrgunit(omsOrgunit);
    }

    private boolean initOrgUnit(OmsOrgunit omsOrgunit) {
        String parentorgnumber = omsOrgunit.getParentorgnumber();
        if (parentorgnumber.equals("0")){
            omsOrgunit.setLevel(1l);
            omsOrgunit.setLongnumber(omsOrgunit.getOrgnumber());
            omsOrgunit.setFirstorgnumber(omsOrgunit.getOrgnumber());
            omsOrgunit.setFirstorgname(omsOrgunit.getOrgname());
        }else {
            OmsOrgunit parentOrgUnit = new OmsOrgunit();
            parentOrgUnit.setOrgnumber(parentorgnumber);
            List<OmsOrgunit> omsOrgunits = selectOmsOrgunitList(parentOrgUnit);
            if (StringUtils.isEmpty(omsOrgunits)){
                return true;
            }
            parentOrgUnit = omsOrgunits.get(0);
            omsOrgunit.setLevel(parentOrgUnit.getLevel()+1);
            omsOrgunit.setLongnumber(parentOrgUnit.getLongnumber()+'!'+omsOrgunit.getOrgnumber());
            OmsOrgunit grandfatherNode = omsOrgunitMapper.findGrandfatherNodeByNumber(parentorgnumber);
            omsOrgunit.setFirstorgnumber(grandfatherNode.getOrgnumber());
            omsOrgunit.setFirstorgname(grandfatherNode.getOrgname());
        }
        return false;
    }

    /**
     * 批量删除结算组织
     * 
     * @param ids 需要删除的结算组织主键
     * @return 结果
     */
    @Override
    public int deleteOmsOrgunitByIds(Long[] ids)
    {
        return omsOrgunitMapper.deleteOmsOrgunitByIds(ids);
    }

    /**
     * 删除结算组织信息
     * 
     * @param id 结算组织主键
     * @return 结果
     */
    @Override
    public int deleteOmsOrgunitById(Long id)
    {
        return omsOrgunitMapper.deleteOmsOrgunitById(id);
    }
}
